<?php
namespace app\common\model;

use app\extra\BaseModel;
use app\extra\DBCont;

class Admin extends BaseModel
{
    use \think\model\concern\SoftDelete;
    protected $deleteTime = 'delete_time';

    protected $auto = ['password'];
    protected $insert = ['status' => 1];
    protected $update = [];
    
    public function login($username, $password){
        $where = [
            'username' => $username,
            'password' => $this->encryption($password),
            'status' => DBCont::NORMAL_STATUS
        ];
        $data = $this->where($where)->find();
        if(empty($data)){
            $this->error = '密码或用户名错误，请重新输入';
            return false;
        } else {
            sysLogs('管理员登陆', '用户：'.$data['nickname'].',账号：'.$data['username'].',于'.showTime($data['create_time']).' 登陆');
            $this->where('id', $data['id'])->update(['login_time'=>time(),'ip'=>request()->ip()]);
            return $data;
        }
    }
    public function encryption($password){
         return md5($password);
    }
    public function setPasswordAttr($value)
    {
        if(empty($value)){
            $value = 'admin123';
        }
        return $this->encryption($value);
    }
    public function getPasswordAttr(&$value, &$data)
    {
        return null;
    }         
    public function setAuthorityAttr($value){
        return serializeByArray($value);
    }
    public function getLoginTimeAttr($value){
        if (empty($value)){
            return null;
        }
        return date('Y-m-d h:i:s', $value);
    }
    public function getAuthorityAttr($value){
        return unSerializeByJson($value);
    }
    public function getUserName($id){
        if (empty($id)){
            return null;
        }
        return $this->where('id', $id)->value('username');
    }
}